from PySide6.QtCore import Qt
from PySide6.QtWidgets import  QWidget, QHBoxLayout, QVBoxLayout

from qfluentwidgets import SingleDirectionScrollArea, SimpleCardWidget, PushButton, TitleLabel, CaptionLabel, LineEdit

from gui.gui_signup import SFormSignup
from gui.gui_signin import SFormSignin
from core.util import loadini_user, clearini_user
from core.log import logger
import  core.model_signin as u
import setting as c


def clearLayout(layout):
    # 移除旧布局中的所有小部件
    while layout.count():
        item = layout.takeAt(0)
        widget = item.widget()
        if widget:
            widget.setParent(None)
        l = item.layout()
        if l:
            clearLayout(l)

class UserCenterInfoCard(SimpleCardWidget):
    """ App information card """

    def __init__(self, parent=None):
        super().__init__(parent)
        self.father = parent
        self.UserCenter_lb = TitleLabel('')
        
        self.signin_btn = PushButton('登录')
        self.signin_btn.clicked.connect(self.onclick_signin)
        self.signup_btn = PushButton('注册')
        self.signup_btn.clicked.connect(self.onclick_signup)
        self.tips_label = CaptionLabel('')
        
        self.sf_signup = SFormSignup(self)
        self.sf_signin = SFormSignin(self)
        
        self.username_label = CaptionLabel('用户名: ')
        self.username_le = LineEdit()
        self.username_le.setReadOnly(True)
        self.email_label = CaptionLabel('Email: ')
        self.email_le = LineEdit()
        self.email_le.setReadOnly(True)
        self.invitation_label = CaptionLabel('邀请码： ')
        self.invitation_le = LineEdit()
        self.invitation_le.setReadOnly(True)
        self.invitation_tips = CaptionLabel(c.TIPS_INVATATION)
        self.logout_btn = PushButton('退出当前用户')
        self.logout_btn.clicked.connect(self.onclick_logout)
        
        self.vBoxLayout = QVBoxLayout(self)
        
        self.hBoxLayout_sign = QHBoxLayout()
        self.hBoxLayout_tips = QHBoxLayout()
        
        self.hBoxLayout_username = QHBoxLayout()
        self.hBoxLayout_email = QHBoxLayout()
        self.hBoxLayout_invatation = QHBoxLayout()
        self.hBoxLayout_invatation_tips = QHBoxLayout()
        self.hBoxLayout_logout = QHBoxLayout()
        
        self.initLayout()
        

    def initLayout(self):
        
        s, u.signin_flag = loadini_user()
        if u.signin_flag:
            logger.info(f"load user: {s}")
            u.signin_username = s[0]
            u.signin_email = s[1]
            u.signin_token = s[2]
            u.signin_invatation = s[3]    
        
        if u.signin_flag:
            self.username_le.setText(s[0])
            self.email_le.setText(s[1])
            self.invitation_le.setText(s[3])
            
            self.hBoxLayout_username.setSpacing(30)
            self.hBoxLayout_username.setContentsMargins(34, 24, 24, 24)
            self.hBoxLayout_username.addWidget(self.username_label, 1)
            self.hBoxLayout_username.addWidget(self.username_le, 4)
            
            self.hBoxLayout_email.setSpacing(30)
            self.hBoxLayout_email.setContentsMargins(34, 24, 24, 24)
            self.hBoxLayout_email.addWidget(self.email_label, 1)
            self.hBoxLayout_email.addWidget(self.email_le, 4)
            
            self.hBoxLayout_invatation.setSpacing(30)
            self.hBoxLayout_invatation.setContentsMargins(34, 24, 24, 24)
            self.hBoxLayout_invatation.addWidget(self.invitation_label, 1)
            self.hBoxLayout_invatation.addWidget(self.invitation_le, 4)
            
            self.hBoxLayout_invatation_tips.setSpacing(3)
            self.hBoxLayout_invatation_tips.setContentsMargins(34, 5, 4, 24)
            self.hBoxLayout_invatation_tips.addWidget(self.invitation_tips, 1)
            
            self.hBoxLayout_logout.setSpacing(30)
            self.hBoxLayout_logout.setContentsMargins(34, 24, 24, 24)
            self.hBoxLayout_logout.addWidget(self.logout_btn)
            
            self.vBoxLayout.addLayout(self.hBoxLayout_username)
            self.vBoxLayout.addLayout(self.hBoxLayout_email)
            self.vBoxLayout.addLayout(self.hBoxLayout_invatation)
            self.vBoxLayout.addLayout(self.hBoxLayout_invatation_tips)
            self.vBoxLayout.addLayout(self.hBoxLayout_logout)
        else:
            self.hBoxLayout_sign.setSpacing(30)
            self.hBoxLayout_sign.setContentsMargins(34, 24, 24, 24)
            self.hBoxLayout_sign.addWidget(self.signin_btn)
            self.hBoxLayout_sign.addWidget(self.signup_btn)
            
            self.hBoxLayout_tips.setSpacing(30)
            self.hBoxLayout_tips.setContentsMargins(34, 24, 24, 24)
            self.hBoxLayout_tips.addWidget(self.tips_label)
            
            self.vBoxLayout.addLayout(self.hBoxLayout_sign, Qt.AlignCenter)
            self.vBoxLayout.addLayout(self.hBoxLayout_tips)
            
    def setTips(self, tips):
        self.tips_label.setText(f"<font color='red'>{tips}</font>")
        
    def onclick_signup(self):
        if c.URL_QA == "":
            self.setTips("请先设置后台 ip 和 port")
            return
        # 计算主窗口中心位置
        UserCenter_window_center = self.LocateCenter()
        new_window_geometry = self.sf_signup.frameGeometry()
        new_window_geometry.moveCenter(UserCenter_window_center)
        
        # 移动新窗口到主窗口中心位置并显示
        self.sf_signup.move(new_window_geometry.topLeft())
        self.sf_signup.Reset()
        self.sf_signup.show()
        
    def onclick_signin(self):
        if c.URL_QA == "":
            self.setTips("请先设置后台 ip 和 port")
            return
        # 计算主窗口中心位置
        UserCenter_window_center = self.LocateCenter()
        new_window_geometry = self.sf_signin.frameGeometry()
        new_window_geometry.moveCenter(UserCenter_window_center)
        
        # 移动新窗口到主窗口中心位置并显示
        self.sf_signin.move(new_window_geometry.topLeft())
        self.sf_signin.Reset()
        self.sf_signin.show()
        
    def LocateCenter(self):
        return  self.father.LocateCenter()
    
    def onclick_logout(self):
        u.signin_flag = False
        u.signin_username = None
        u.signin_email = None
        u.signin_token = None
        u.signin_invatation = None 
        
        self.username_le.setText("")
        self.email_le.setText("")
        self.invitation_le.setText("")
        clearini_user()
        self.ResetSignin()
        
        
    def ResetSignin(self):
        clearLayout(self.vBoxLayout)
        self.initLayout()
        
    def close(self):
        self.father.close()
        
        
class UserCenterInterface(SingleDirectionScrollArea):

    def __init__(self, parent=None):
        super().__init__(parent=None)
        self.father = parent
        self.view = QWidget(self)

        self.vBoxLayout = QVBoxLayout(self.view)
        self.UserCenterCard = UserCenterInfoCard(self)
        self.UserCenterCard.setMaximumWidth(c.card_width)
        self.setWidget(self.view)
        self.setWidgetResizable(True)
        self.setObjectName('UserCenterInterface')

        self.vBoxLayout.setSpacing(10)
        self.vBoxLayout.setContentsMargins(0, 0, 15, 0)
        self.vBoxLayout.addWidget(self.UserCenterCard, 0, Qt.AlignTop)

        self.setStyleSheet("QScrollArea {border: none; background:transparent}")
        self.view.setStyleSheet('QWidget {background:transparent}')
        
    def show(self):
        print("show UserCenterInterface")
        
    def close(self):
        self.father.close()
        
    def LocateCenter(self):
        return  self.father.LocateCenter()